import LogicFlow from '@logicflow/core';
import { Extension } from '@logicflow/core';

import StartEvent, { StartEventModel, StartEventView } from './elements/StartEvent';
import EndEvent, { EndEventView, EndEventModel } from './elements/EndEvent';
import ExclusiveGateway, { ExclusiveGatewayView, ExclusiveGatewayModel } from './elements/ExclusiveGateway';
import UserTask, { UserTaskView, UserTaskModel } from './elements/UserTask';
import ServiceTask, { ServiceTaskView, ServiceTaskModel } from './elements/ServiceTask';
import SequenceFlow, { SequenceFlowView, SequenceFlowModel } from './elements/SequenceFlow';

import { theme } from './constant';

import { convertXml2GraphData, convertGraphData2Xml } from './utils';

const FlowableSupport:Extension =  {
  pluginName: 'FlowableSupport',
  install(lf:LogicFlow) {
    lf.setTheme(theme);
    lf.register(StartEvent);
    lf.register(EndEvent);
    lf.register(ExclusiveGateway);
    lf.register(UserTask);
    lf.register(ServiceTask);
    // 支持自定义bpmn元素的边
    if (!lf.options.customBpmnEdge) {
      lf.register(SequenceFlow);
      lf.setDefaultEdgeType('sequenceFlow');
    };
    // 数据转换
    lf.adapterIn = (bpmnXml:any)=>{
      const graphData = convertXml2GraphData(bpmnXml);
      lf.props = ref(graphData.props).value;
      return graphData;
    };
    lf.adapterOut = (graphData:any)=>{
      graphData.props = JSON.parse(JSON.stringify(lf.props));
      return convertGraphData2Xml(graphData);
    }
  }
}

export {
  FlowableSupport
};
